/**
 * @file pin_init.c
 * @author Blues Jiang (jiangrn@breo.com.cn)
 * @brief Init used pin in Dream 5C
 * @version 0.1
 * @date 2021-12-21
 *
 * @copyright Copyright (c) 2021
 *
 */

/* Used Pin in Dream 5C */
#include <rtthread.h>
#include <rtdevice.h>
#include <n32_msp.h>
#include <pin_dev_cfg.h>
#include <drv_gpio.h>

void power_ctrl_pin_init(void)
{
}

void display_pin_init(void)
{
    rt_pin_mode(PIN_DISPLAY_RST, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_DISPLAY_DC, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_DISPLAY_SPI_CS, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_DISPLAY_SPI_CS, PIN_MODE_OUTPUT);
    rt_pin_write(PIN_DISPLAY_SPI_CS, PIN_HIGH);
    rt_pin_write(PIN_DISPLAY_DC, PIN_LOW);
}

void switch_pin_init(void)
{
    #if (MATRIX_SCAN_DIRCTION == DIODE_ROW2COL)
    rt_pin_mode(PIN_COLUMN1, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_COLUMN2, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_COLUMN3, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_COLUMN4, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_COLUMN5, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_COLUMN6, PIN_MODE_INPUT_PULLDOWN);

    rt_pin_mode(PIN_ROW1, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_ROW2, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_ROW3, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_ROW4, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_ROW5, PIN_MODE_OUTPUT);

    #elif(MATRIX_SCAN_DIRCTION == DIODE_COL2ROW)
    rt_pin_mode(PIN_COLUMN1, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_COLUMN2, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_COLUMN3, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_COLUMN4, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_COLUMN5, PIN_MODE_OUTPUT);
    rt_pin_mode(PIN_COLUMN6, PIN_MODE_OUTPUT);

    rt_pin_mode(PIN_ROW1, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_ROW2, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_ROW3, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_ROW4, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_ROW5, PIN_MODE_INPUT_PULLDOWN);
    #endif

    rt_pin_mode(PIN_ENCODER1A, PIN_MODE_INPUT);
    rt_pin_mode(PIN_ENCODER1B, PIN_MODE_INPUT);
    rt_pin_mode(PIN_ENCODER2A, PIN_MODE_INPUT);
    rt_pin_mode(PIN_ENCODER2B, PIN_MODE_INPUT);
    rt_pin_mode(PIN_ENCODER3A, PIN_MODE_INPUT);
    rt_pin_mode(PIN_ENCODER3B, PIN_MODE_INPUT);

}

void gpio_pin_init(void)
{
    power_ctrl_pin_init();
    display_pin_init();
    switch_pin_init();
    // return RT_EOK;
}
INIT_PREV_EXPORT(gpio_pin_init);
